//
//  ViewController.swift
//  Checklists
//
//  Created by M.I. Hollemans on 15/09/14.
//  Copyright (c) 2014 Razeware. All rights reserved.
//

import UIKit

class ChecklistViewController: UITableViewController, ItemDetailViewControllerDelegate {

	var	checklist:Checklist!

  override func viewDidLoad() {
    super.viewDidLoad()

		tableView.rowHeight = 44
		title = checklist.name
  }
	
	override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
		if segue.identifier == "AddItem" {
			let navigationController = segue.destinationViewController as! UINavigationController
			let controller = navigationController.topViewController as! ItemDetailViewController
			controller.delegate = self
		} else if segue.identifier == "EditItem" {
			let navigationController = segue.destinationViewController as! UINavigationController
			let controller = navigationController.topViewController as! ItemDetailViewController
			controller.delegate = self
			
			if let indexPath = tableView.indexPathForCell(sender as! UITableViewCell) {
				controller.itemToEdit = checklist.items[indexPath.row]
			}
		}
	}

  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return checklist.items.count
  }
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem")! as UITableViewCell
    
    let item = checklist.items[indexPath.row]
    
    configureTextForCell(cell, withChecklistItem: item)
    configureCheckmarkForCell(cell, withChecklistItem: item)
    configureDueDateForCell(cell, withChecklistItem: item)
		
    return cell
  }
  
  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
      let item = checklist.items[indexPath.row]
      item.toggleChecked()
      
      configureCheckmarkForCell(cell, withChecklistItem: item)
    }
    tableView.deselectRowAtIndexPath(indexPath, animated: true)		
  }

  override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    checklist.items.removeAtIndex(indexPath.row)
    
    let indexPaths = [indexPath]
    tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)		
  }

  func configureCheckmarkForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) {
		let label = cell.viewWithTag(1001) as! UILabel
		
    if item.checked {
			label.text = "✓"
    } else {
			label.text = ""
		}
		label.textColor = view.tintColor
  }

  func configureTextForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) {
    let label = cell.viewWithTag(1000) as! UILabel
//    label.text = item.text
		label.text = "\(item.itemID): \(item.text)"
  }
	
	func configureDueDateForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) {
		let label = cell.viewWithTag(1002) as! UILabel
		if item.shouldRemind {
			label.hidden = false
			let dateFormatter = NSDateFormatter()
			dateFormatter.dateStyle = .MediumStyle
			dateFormatter.timeStyle = .ShortStyle
			label.text = dateFormatter.stringFromDate(item.dueDate)
			
			if item.dueDateValid() {
				label.textColor = UIColor.greenColor()
			} else {
				label.textColor = UIColor.redColor()
			}
		} else {
			label.hidden = true
		}
	}

  func itemDetailViewControllerDidCancel(controller: ItemDetailViewController) {
    dismissViewControllerAnimated(true, completion: nil)
  }
  
  func itemDetailViewController(controller: ItemDetailViewController, didFinishAddingItem item: ChecklistItem) {
    let newRowIndex = checklist.items.count
    
    checklist.items.append(item)
    
    let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
    let indexPaths = [indexPath]
    tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
    
		checklist.sortTodoLists()
		tableView.reloadData()
    dismissViewControllerAnimated(true, completion: nil)		
  }
	
	func itemDetailViewController(controller: ItemDetailViewController, didFinishEditingItem item: ChecklistItem) {
		if let index = checklist.items.indexOf({$0 === item}) {
			let indexPath = NSIndexPath(forRow: index, inSection: 0)
			if let cell = tableView.cellForRowAtIndexPath(indexPath) {
				configureTextForCell(cell, withChecklistItem: item)
				configureDueDateForCell(cell, withChecklistItem: item)
				checklist.sortTodoLists()
				tableView.reloadData()
			}
		}
		dismissViewControllerAnimated(true, completion: nil)		
	}
}
